package com.wxit.eduservice.controller;


import com.wxit.commonutils.R;
import com.wxit.eduservice.client.VodClient;
import com.wxit.eduservice.entity.EduVideo;
import com.wxit.eduservice.service.EduVideoService;
import com.wxit.exception.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author 吴杰
 * @since 2021-11-09
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //删除小节，需要完善
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){

        //根据小节id查询视频id
        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();

        if (!StringUtils.isEmpty(videoSourceId)){
            //调用远程方法实现删除
            R result = vodClient.removeAliyunVideo(videoSourceId);
            if (result.getCode() == 20001){
                throw new GuliException(20001,"删除视频失败");
            }
        }
        eduVideoService.removeById(id);
        return R.ok();
    }

}

